package com.ruoyi.shop.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.ShopSourceTypeEnums;
import com.ruoyi.common.enums.dict.SysYesNo;
import com.ruoyi.common.utils.bean.Builder;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.shop.mapper.ShopPointsIncomeMapper;
import com.ruoyi.shop.domain.ShopPointsIncome;
import com.ruoyi.shop.service.IShopPointsIncomeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 积分获取记录Service业务层处理
 * 
 * @author liuwp
 * @date 2025-11-24
 */
@Service
public class ShopPointsIncomeServiceImpl implements IShopPointsIncomeService 
{
    @Autowired
    private ShopPointsIncomeMapper shopPointsIncomeMapper;

    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询积分获取记录
     * 
     * @param id 积分获取记录主键
     * @return 积分获取记录
     */
    @Override
    public ShopPointsIncome selectShopPointsIncomeById(Long id)
    {
        return shopPointsIncomeMapper.selectShopPointsIncomeById(id);
    }

    /**
     * 查询积分获取记录列表
     * 
     * @param shopPointsIncome 积分获取记录
     * @return 积分获取记录
     */
    @Override
    public List<ShopPointsIncome> selectShopPointsIncomeList(ShopPointsIncome shopPointsIncome)
    {
        return shopPointsIncomeMapper.selectShopPointsIncomeList(shopPointsIncome);
    }

    /**
     * 根据业务主键查询一条记录：积分获取记录
     *
     * @param shopPointsIncome 积分获取记录
     * @return 积分获取记录
     */
    @Override
    public ShopPointsIncome selectOneShopPointsIncome(ShopPointsIncome shopPointsIncome)
    {
        return shopPointsIncomeMapper.selectOneShopPointsIncome(shopPointsIncome);
    }

    /**
     * 新增积分获取记录
     * 
     * @param shopPointsIncome 积分获取记录
     * @return 结果
     */
    @Override
    public int insertShopPointsIncome(ShopPointsIncome shopPointsIncome)
    {
        return shopPointsIncomeMapper.insertShopPointsIncome(shopPointsIncome);
    }

    /**
     * 修改积分获取记录
     * 
     * @param shopPointsIncome 积分获取记录
     * @return 结果
     */
    @Override
    public int updateShopPointsIncome(ShopPointsIncome shopPointsIncome)
    {
        return shopPointsIncomeMapper.updateShopPointsIncome(shopPointsIncome);
    }

    @Override
    public int updateShopPointsIncomeCondition(ShopPointsIncome shopPointsIncome, ShopPointsIncome shopPointsIncomeCondition)
    {
        return shopPointsIncomeMapper.updateShopPointsIncomeCondition(shopPointsIncome, shopPointsIncomeCondition);
    }

    /**
     * 批量删除积分获取记录
     * 
     * @param ids 需要删除的积分获取记录主键
     * @return 结果
     */
    @Override
    public int deleteShopPointsIncomeByIds(Long[] ids)
    {
        return shopPointsIncomeMapper.deleteShopPointsIncomeByIds(ids);
    }

    /**
     * 删除积分获取记录信息
     * 
     * @param id 积分获取记录主键
     * @return 结果
     */
    @Override
    public int deleteShopPointsIncomeById(Long id)
    {
        return shopPointsIncomeMapper.deleteShopPointsIncomeById(id);
    }

    /**
     * 条件删除积分获取记录
     *
     * @param shopPointsIncome 积分获取记录
     * @return 结果
     */
    @Override
    public int deleteShopPointsIncomeByCondition(ShopPointsIncome shopPointsIncome)
    {
        return shopPointsIncomeMapper.deleteShopPointsIncomeByCondition(shopPointsIncome);
    }

    @Override
    @Transactional
    public AjaxResult pointsOperationRecord(Long userId, Long points, ShopSourceTypeEnums shopSourceTypeEnums, String remark, String assetsId) {
        // 余额判断
        SysUser user = userMapper.selectUserById(userId);
        if (user == null) {
            return AjaxResult.error("用户不存在");
        }
        if (user.getPoints() == null) {
            user.setPoints(0L);
        }

        if (points.longValue() < 0){
            if (Math.abs(points.longValue()) > user.getPoints().longValue()) {
                return AjaxResult.error("积分余额不足");
            }
        }

        ShopPointsIncome shopPointsIncome = Builder.of(ShopPointsIncome::new)
                .with(ShopPointsIncome::setUserId, userId)
                .with(ShopPointsIncome::setPoints, points)
                .with(ShopPointsIncome::setSourceType, shopSourceTypeEnums.getValue())
                .with(ShopPointsIncome::setSourceId, assetsId)
                .with(ShopPointsIncome::setDescription, remark)
                .with(ShopPointsIncome::setStatus, SysYesNo.YES.getValue())
                .build();
        insertShopPointsIncome(shopPointsIncome);

        // 扣用户积分
        // 如果是消息积分这里要传负值
        user.setPoints(user.getPoints() + points);
        userMapper.updateUser(user);

        return AjaxResult.success(shopPointsIncome);
    }

}
